{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. UIautomation库\n",
    "\n",
    "> https://github.com/yinkaisheng/Python-UIAutomation-for-Windows/blob/master/readme_cn.md"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ControlType: PaneControl    ClassName: #32769    AutomationId:     Rect: (0,0,1920,1080)[1920x1080]    Name: '桌面 1'    Handle: 0x10010(65552)\n",
      "无标题 - 记事本\n",
      "current text: Hello\n",
      "World\n"
     ]
    }
   ],
   "source": [
    "# 测试UIautomation代码\n",
    "# -*- coding: utf-8 -*-\n",
    "import subprocess\n",
    "import uiautomation as auto\n",
    "\n",
    "def test():\n",
    "    print(auto.GetRootControl())\n",
    "    subprocess.Popen('notepad.exe')\n",
    "    # 首先从桌面的第一层子控件中找到记事本程序的窗口WindowControl，再从这个窗口查找子控件\n",
    "    notepadWindow = auto.WindowControl(searchDepth=1, ClassName='Notepad')\n",
    "    print(notepadWindow.Name)\n",
    "    notepadWindow.SetTopmost(True)\n",
    "    # 查找notepadWindow所有子孙控件中的第一个EditControl，因为EditControl是第一个子控件，可以不指定深度\n",
    "    edit = notepadWindow.EditControl()\n",
    "    try:\n",
    "        # 获取EditControl支持的ValuePattern，并用Pattern设置控件文本为\"Hello\"\n",
    "        edit.GetValuePattern().SetValue('Hello')# or edit.GetPattern(auto.PatternId.ValuePattern)\n",
    "    except auto.comtypes.COMError as ex:\n",
    "        # 如果遇到COMError, 一般是没有以管理员权限运行Python, 或者这个控件没有实现pattern的方法(如果是这种情况，基本没有解决方法)\n",
    "        # 大多数情况不需要捕捉COMError，如果遇到了就加到try block\n",
    "        pass\n",
    "    edit.SendKeys('{Ctrl}{End}{Enter}World')# 在文本末尾打字\n",
    "    print('current text:', edit.GetValuePattern().Value)# 获取当前文本\n",
    "    # 先从notepadWindow的第一层子控件中查找TitleBarControl, \n",
    "    # 然后从TitleBarControl的子孙控件中找第二个ButtonControl, 即最大化按钮，并点击按钮\n",
    "    notepadWindow.TitleBarControl(Depth=1).ButtonControl(foundIndex=2).Click()\n",
    "    # 从notepadWindow前两层子孙控件中查找Name为'关闭'的按钮并点击按钮\n",
    "    notepadWindow.ButtonControl(searchDepth=2, Name='关闭').Click()\n",
    "    # 这时记事本弹出是否保存提示，按热键Alt+N不保存退出。\n",
    "    auto.SendKeys('{Alt}n')\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    test()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 微信窗口\n",
    "\n",
    "双击聊天人弹出单独的窗口后"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-4-35aa49eaf603>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     26\u001b[0m     \u001b[0mspying_window\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSendKey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m13\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 点击Enter按键ASCⅡ\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     27\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 28\u001b[1;33m \u001b[0mformer_msg_list\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mget_msg_list\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 上一消息列表状态消息\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     29\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     30\u001b[0m     \u001b[0mlatest_msg_list\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mget_msg_list\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-4-35aa49eaf603>\u001b[0m in \u001b[0;36mget_msg_list\u001b[1;34m()\u001b[0m\n\u001b[0;32m     10\u001b[0m     \u001b[0mmsg_list\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     11\u001b[0m     \u001b[1;31m# 找到窗口的list部件然后获取内容\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 12\u001b[1;33m     \u001b[0mmsg_ctrl_list\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mspying_window\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mListControl\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mGetChildren\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     13\u001b[0m     \u001b[1;31m# print(f\" Test1: the first msg_ctrl is : {msg_ctrl_list[0]} \\n Test2: the last msg_ctrl is : {msg_ctrl_list[-1]} \\n Test3: the length of msg_List is : {len(msg_ctrl_list)} \") #\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     14\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mmsg_ctrl\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mmsg_ctrl_list\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\Anaconda3\\lib\\site-packages\\uiautomation\\uiautomation.py\u001b[0m in \u001b[0;36mGetChildren\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m   6342\u001b[0m         \"\"\"\n\u001b[0;32m   6343\u001b[0m         \u001b[0mchildren\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 6344\u001b[1;33m         \u001b[0mchild\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mGetFirstChildControl\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   6345\u001b[0m         \u001b[1;32mwhile\u001b[0m \u001b[0mchild\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   6346\u001b[0m             \u001b[0mchildren\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mchild\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\Anaconda3\\lib\\site-packages\\uiautomation\\uiautomation.py\u001b[0m in \u001b[0;36mGetFirstChildControl\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m   6288\u001b[0m         \u001b[0mReturn\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m`\u001b[0m\u001b[0mControl\u001b[0m\u001b[0;31m`\u001b[0m \u001b[0msubclass\u001b[0m \u001b[1;32mor\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   6289\u001b[0m         \"\"\"\n\u001b[1;32m-> 6290\u001b[1;33m         \u001b[0mele\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_AutomationClient\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mViewWalker\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mGetFirstChildElement\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mElement\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   6291\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mControl\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mCreateControlFromElement\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mele\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   6292\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\Anaconda3\\lib\\site-packages\\uiautomation\\uiautomation.py\u001b[0m in \u001b[0;36mElement\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m   6221\u001b[0m         \"\"\"\n\u001b[0;32m   6222\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_element\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 6223\u001b[1;33m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mRefind\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmaxSearchSeconds\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mTIME_OUT_SECOND\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msearchIntervalSeconds\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msearchInterval\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   6224\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_element\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   6225\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\Anaconda3\\lib\\site-packages\\uiautomation\\uiautomation.py\u001b[0m in \u001b[0;36mRefind\u001b[1;34m(self, maxSearchSeconds, searchIntervalSeconds, raiseException)\u001b[0m\n\u001b[0;32m   6469\u001b[0m         \u001b[0mReturn\u001b[0m \u001b[0mbool\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;32mTrue\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mfind\u001b[0m\u001b[1;33m.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   6470\u001b[0m         \"\"\"\n\u001b[1;32m-> 6471\u001b[1;33m         \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mExists\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmaxSearchSeconds\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msearchIntervalSeconds\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;32mFalse\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mraiseException\u001b[0m \u001b[1;32melse\u001b[0m \u001b[0mDEBUG_EXIST_DISAPPEAR\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   6472\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mraiseException\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   6473\u001b[0m                 \u001b[0mLogger\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mColorfullyLog\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'<Color=Red>Find Control Timeout({}s): </Color>{}'\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmaxSearchSeconds\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mGetColorfulSearchPropertiesStr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\Anaconda3\\lib\\site-packages\\uiautomation\\uiautomation.py\u001b[0m in \u001b[0;36mExists\u001b[1;34m(self, maxSearchSeconds, searchIntervalSeconds, printIfNotExist)\u001b[0m\n\u001b[0;32m   6411\u001b[0m         \u001b[1;31m# Use same timeout(s) parameters for resolve all parents\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   6412\u001b[0m         \u001b[0mprev\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msearchFromControl\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 6413\u001b[1;33m         \u001b[1;32mif\u001b[0m \u001b[0mprev\u001b[0m \u001b[1;32mand\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mprev\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_element\u001b[0m \u001b[1;32mand\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mprev\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mExists\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmaxSearchSeconds\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msearchIntervalSeconds\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   6414\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mprintIfNotExist\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mDEBUG_EXIST_DISAPPEAR\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   6415\u001b[0m                 \u001b[0mLogger\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mColorfullyLog\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mGetColorfulSearchPropertiesStr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;34m'<Color=Red> does not exist.</Color>'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\Anaconda3\\lib\\site-packages\\uiautomation\\uiautomation.py\u001b[0m in \u001b[0;36mExists\u001b[1;34m(self, maxSearchSeconds, searchIntervalSeconds, printIfNotExist)\u001b[0m\n\u001b[0;32m   6419\u001b[0m             \u001b[0mstartDateTime\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdatetime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   6420\u001b[0m         \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 6421\u001b[1;33m             \u001b[0mcontrol\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mFindControl\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msearchFromControl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_CompareFunction\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msearchDepth\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;32mFalse\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfoundIndex\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   6422\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mcontrol\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   6423\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_element\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcontrol\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mElement\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\Anaconda3\\lib\\site-packages\\uiautomation\\uiautomation.py\u001b[0m in \u001b[0;36mFindControl\u001b[1;34m(control, compare, maxDepth, findFromSelf, foundIndex)\u001b[0m\n\u001b[0;32m   8354\u001b[0m         \u001b[0mcontrol\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mGetRootControl\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   8355\u001b[0m     \u001b[0mtraverseCount\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 8356\u001b[1;33m     \u001b[1;32mfor\u001b[0m \u001b[0mchild\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdepth\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mWalkControl\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcontrol\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfindFromSelf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmaxDepth\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   8357\u001b[0m         \u001b[0mtraverseCount\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   8358\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mcompare\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mchild\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdepth\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\Anaconda3\\lib\\site-packages\\uiautomation\\uiautomation.py\u001b[0m in \u001b[0;36mWalkControl\u001b[1;34m(control, includeTop, maxDepth)\u001b[0m\n\u001b[0;32m   8232\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mlastControl\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   8233\u001b[0m             \u001b[1;32myield\u001b[0m \u001b[0mlastControl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdepth\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 8234\u001b[1;33m             \u001b[0mchild\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlastControl\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mGetNextSiblingControl\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   8235\u001b[0m             \u001b[0mcontrolList\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mdepth\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mchild\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   8236\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mdepth\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m \u001b[1;33m<\u001b[0m \u001b[0mmaxDepth\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\Anaconda3\\lib\\site-packages\\uiautomation\\uiautomation.py\u001b[0m in \u001b[0;36mGetNextSiblingControl\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m   6302\u001b[0m         \u001b[0mReturn\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m`\u001b[0m\u001b[0mControl\u001b[0m\u001b[0;31m`\u001b[0m \u001b[0msubclass\u001b[0m \u001b[1;32mor\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   6303\u001b[0m         \"\"\"\n\u001b[1;32m-> 6304\u001b[1;33m         \u001b[0mele\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_AutomationClient\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mViewWalker\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mGetNextSiblingElement\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mElement\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   6305\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mControl\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mCreateControlFromElement\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mele\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   6306\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "# wechat\n",
    "import uiautomation\n",
    "import time\n",
    "\n",
    "\"\"\"\n",
    "# TODO 用正则表达式提取文本内需要的信息\n",
    "import re\n",
    "def to_list_table(txt_msg:str, path=\"./date/xxx.csv\"):\n",
    "    table = []\n",
    "    ? uid = re.findall(\"(\\d{17,}(\\d|X|x))\",txt_msg)\n",
    "    ? uid = re.findall(\"(\\d{17,}[\\dXx])\",txt_msg)\n",
    "    ......\n",
    "    table.apend([uid,ct,time,lab_name...])\n",
    "    with open(path) as f:\n",
    "        f.write(table, 'a', encoding='utf-8_sig')\n",
    "        for i in table:\n",
    "            send_text_msg(f\"身份证{i[0]},已经存档\", trans_window_name=\"xxxx群\")\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "# 定位微信主窗口，获取窗口句柄\n",
    "spying_window_name = \"文件传输助手\"\n",
    "spying_window = uiautomation.WindowControl(searchDepth=1, Name=spying_window_name)\n",
    "\n",
    "def get_msg_list():\n",
    "    msg_list = [] \n",
    "    # 找到窗口的list部件然后获取内容\n",
    "    msg_ctrl_list = spying_window.ListControl().GetChildren()\n",
    "    # print(f\" Test1: the first msg_ctrl is : {msg_ctrl_list[0]} \\n Test2: the last msg_ctrl is : {msg_ctrl_list[-1]} \\n Test3: the length of msg_List is : {len(msg_ctrl_list)} \") #\n",
    "    for msg_ctrl in msg_ctrl_list:\n",
    "        msg_list.append(msg_ctrl.Name)\n",
    "        # print(f\" Test0: the msg_ctrl is : {msg_ctrl}\")  \n",
    "        # \"Test0: the msg_ctrl is : ControlType: ListItemControl    ClassName:     AutomationId:     Rect: (993,-3612,1543,-3578)[550x34]    Name: '星期天 22:40'    Handle: 0x0(0)\"\n",
    "        # ......\n",
    "        # \"Test0: the msg_ctrl is : ControlType: ListItemControl    ClassName:     AutomationId:     Rect: (993,-2237,1543,-2106)[550x131]    Name: '[文件]'    Handle: 0x0(0)\"\n",
    "        # ? 排除文本以外内容 Name's category: '查看更多消息', '星期天 22:40', '[动画表情]', '[文件]', '[图片]', '昨天 19:22', '[OK]', '20:14', '字符串信息之类的东西'; \n",
    "    # print(f\" Test4: msg_list is : {msg_list}\") #\n",
    "    return msg_list\n",
    "\n",
    "def send_text_msg(text_msg:str, trans_window_name:str):\n",
    "    # 双击得到单独窗口的需要转发的群\n",
    "    trans_window = uiautomation.WindowControl(searchDepth=1, Name=trans_window_name)\n",
    "    trans_window.SendKeys(text_msg) # 写入消息\n",
    "    trans_window.SendKey(13) # 点击Enter按键ASCⅡ\n",
    "\n",
    "former_msg_list = get_msg_list() # 上一消息列表状态消息\n",
    "while True:\n",
    "    latest_msg_list = get_msg_list()\n",
    "    if latest_msg_list[-1] != former_msg_list[-1]: # 末条消息的Name和上一条消息的Name不同\n",
    "        new_msg = latest_msg_list[-1]\n",
    "        print(new_msg , f\"最后一次列表长度{len(latest_msg_list)}\")\n",
    "        # send_text_msg(new_msg)\n",
    "        former_msg_list = get_msg_list()\n",
    "        time.sleep(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.查看所有窗口句柄和名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "67862 QQ\n",
      "66494 小火箭托盘加速\n",
      "197656 SCM\n",
      "657810 ● UIautomation-1.ipynb - Visual Studio Code\n",
      "853572 [python]微信自动回复-通用窗口操作接口uiautomation_哔哩哔哩_bilibili — Mozilla Firefox\n",
      "8129044 伊卡酱的播种船等3个会话\n",
      "3739266 文件传输助手\n",
      "3278926 微信\n",
      "132036 NVIDIA GeForce Overlay\n",
      "131162 Microsoft Text Input Application\n",
      "197978 设置\n",
      "66840 设置\n",
      "67158 Killer Intelligence Center\n",
      "196830 Program Manager\n"
     ]
    }
   ],
   "source": [
    "import win32gui\n",
    "\n",
    "hwnd_title = dict()\n",
    "\n",
    "def get_all_hwnd(hwnd, mouse):\n",
    "    if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):\n",
    "        hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)})\n",
    "\n",
    "win32gui.EnumWindows(get_all_hwnd, 0)    \n",
    "\n",
    "for h, t in hwnd_title.items():\n",
    "    if t != \"\":\n",
    "        print(h, t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "97ae724bfa85b9b34df7982b8bb8c7216f435b92902d749e4263f71162bea840"
  },
  "kernelspec": {
   "display_name": "Python 3.8.3 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
